﻿#include "FITKAcousticsSensors.h"
#include "FITKAbstractFESubSys.h"
#include "FITKAcousticsDiscModel.h"
#include "FITKAcousticsDiscNodeList.h"
#include "FITKAcousticsFEModel.h"

#include "FITK_Kernel/FITKCore/FITKDataRepo.h"
#include "FITK_Kernel/FITKCore/FITKVec3D.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Interface/FITKInterfaceModel/FITKAbstractElement.h"

namespace Acoustics
{

    FITKAcousticsSensors::FITKAcousticsSensors()
    {
    }

    FITKAcousticsSensors::~FITKAcousticsSensors()
    {
    }

    void FITKAcousticsSensors::setFESubSysObjID(int objID)
    {
        //设置连接对象id-FE子系统
        _feSubSysID = objID;
    }

    int FITKAcousticsSensors::getFESubSysObjID()
    {
        //获取连接对象id-FE子系统
        return _feSubSysID;
    }

    FITKAbstractFESubSys* FITKAcousticsSensors::getFESubSysObj()
    {
        //获取连接对象数据-FE子系统
        return FITKDATAREPO->getTDataByID<FITKAbstractFESubSys>(_feSubSysID);
    }

    void FITKAcousticsSensors::setConnection(int nodeID, int elemID)
    {
        //设置连接数据 NodeID, ElemID
        _connection.first = nodeID;
        _connection.second = elemID;
    }

    QPair<int, int> FITKAcousticsSensors::getConnection()
    {
        //获取连接数据 NodeID, ElemID
        return _connection;
    }

    void FITKAcousticsSensors::setConnectionType(SensorConnectionType type)
    {
        //设置连接类型
        _connectionType = type;
    }

    FITKAcousticsSensors::SensorConnectionType FITKAcousticsSensors::getConnectionType()
    {
        //获取连接类型
        return _connectionType;
    }

    void FITKAcousticsSensors::setSensorResultType(SensorResultType type)
    {
        //设置传感器结果类型
        _resultType = type;
    }

    FITKAcousticsSensors::SensorResultType FITKAcousticsSensors::getSensorResultType()
    {
        //获取传感器结果类型
        return _resultType;
    }

    void FITKAcousticsSensors::setGlobalOrientation(double* orientation)
    {
        //设置全局方向
        _globalOrientation[0] = orientation[0];
        _globalOrientation[1] = orientation[1];
        _globalOrientation[2] = orientation[2];
    }

    void FITKAcousticsSensors::getGlobalOrientation(double* orientation)
    {
        //获取全局方向
        orientation[0] = _globalOrientation[0];
        orientation[1] = _globalOrientation[1];
        orientation[2] = _globalOrientation[2];
    }

    void FITKAcousticsSensors::getLocation(double* location)
    {
        if (!location) return;
        //获取节点数据
        Acoustics::FITKAcousticsDiscModel* discModel = FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
        if (!discModel) return;
        Acoustics::FITKAcousticsDiscNodeList* nodeList = discModel->getNodes();
        if (!nodeList) return;
        //获取位置
        if (_connectionType == SensorConnectionType::SensorConnectionNode)
        {
            //获取节点位置
            int nodeID = _connection.first;
            Core::FITKNode* node = nodeList->getNodeByID(nodeID);
            if (!node) return;
            location[0] = node->x();
            location[1] = node->y();
            location[2] = node->z();
        }
        else if (_connectionType == SensorConnectionType::SensorConnectionElement)
        {
            //获取单元数据
            Acoustics::FITKAcousticsFEModel* feModel = discModel->getFEModel();
            if (!feModel) return;
            Acoustics::FITKAcousticsFESubSysElementList* elementList = feModel->getFESubSysElementList();
            if (!elementList) return;
            //获取单元
            int elementID = _connection.second;
            Interface::FITKAbstractElement* element = elementList->getElementByID(elementID);
            if (!element) return;
            int dim = element->getElementDim();
            QList<int> nodeIDs = element->getAllNodes();
            //获取位置
            double cumulative[3] = { 0,0,0 };
            int count = 0;
            for (int nID : nodeIDs)
            {
                Core::FITKNode* node = nodeList->getNodeByID(nID);
                if (!node) return;
                ++count;
                cumulative[0] += node->x();
                cumulative[1] += node->y();
                cumulative[2] += node->z();
            }
            location[0] = cumulative[0] / count;
            location[1] = cumulative[1] / count;
            location[2] = cumulative[2] / count;
        }
    }
}


